/**
 * 登录，注册
 */
define(function(require, exports, module){
	var $ = require("jquery");
	require("lib/jquery/jquery.validate.min.js");
	require("lib/common/CommonUtil.js");
	require("lib/common/common.js");
	
	$.validator.addMethod('mobile', function(e, a){
		if(e){
			var phoneExc = /^(13|14|15|16|17|18)[0-9][0-9]{8}$/;
			return phoneExc.test(e);
		}
		return true;
	});
	
	$.validator.addMethod('nonsymbol', function(e, a){
		var nonExc = /['"%<]+/g;
		return !nonExc.test(e);
	});
	
	$("#loginForm").validate({
    	rules: {
    	   	name: {
    	    	required: true,
    	    	nonsymbol: true
    	   	},
    	   	mobile: {
    	    	required: false,
    	    	mobile: true
    	   	},
    	   	email: {
    	    	required: false,
    	    	email: 5
    	   	},
    	   	qq: {
    	    	required: false,
    	    	digits: true,
    	    	rangelength: [5, 12]
    	   	}
	  	},
    	messages: {
    		name: {
    	    	required: "请输入您的姓名/昵称",
    	    	nonsymbol: "姓名/昵称不能包含特殊字符['\"%<]"
    	   	},
    	   	mobile: {
    	   		mobile: "手机号码格式不正确"
    	   	},
    	   	eamil: {
    	   		email: "邮箱格式不正确"
    	   	},
    	   	qq: {
    	   		rangelength: jQuery.format("QQ号码位数应在{0}-{1}之间")
    	   	}
		}
    });
	
	$('.form-input input').on('focus', function(){
		$(this).parent().parent().css({'border': '1px double #F49B74'});
	});
	
	$('.form-input input').on('blur', function(){
		$(this).parent().parent().css({'border': '1px double #EFEFEF'});
	});
	
	$('#btn_login').click(function(){
		var $form = $("#loginForm");
	    if($form.valid()){
	    	userId = top.pageid;
	    	top.user = {
	    		userId: userId,
	    		name: $('#name').val(),
	    		mobile: $('#mobile').val(),
	    		email: $('#email').val(),
	    		qq: $('#qq').val(),
	    		sex: $('#sex').val(),
	    		header: $('#header').val()
	    	};
	    	closeDialog();
	    }
	});
			
	var closeDialog = function(){
		try{
			top.data.loginDialog && top.data.loginDialog.close();
		}catch(e){
			console.info("close login Dialog Error, e");
		}
		top.saveUserInfo();
		top.initChat();
	};
	
	
	(function() {
		var options = '', value = "pages/chats/images/headImage/00";
		for ( var i = 1; i < 83; i++) {
			var n = i < 10 ? '0' + i : i;
			options += '<option value="'+value+n+'.gif">00'+n+'.gif</option>';
		}
		$('#header').append(options).on("change", function(){
			$('#headImage').html('<img src="'+top.data.ctx+'/'+$(this).val()+'">');
		});
		$('#headImage').html('<img src="'+top.data.ctx+'/pages/chats/images/headImage/default.png">');
	})();
});